package floappcontest.core.backbeans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import floappcontest.core.beans.Component;
import floappcontest.core.beans.Contest;
import floappcontest.core.control.Control;
import floappcontest.core.dao.factory.HibernateDAOFactory;
import floappcontest.core.exception.FloAppException;
import floappcontest.util.converters.ComponentConverter;

/**
 * The ContestBean is a BackBean (JavaBean) related with the 
 * contest events in the pages.  
 */
public class ContestBean {
	
	private static final String FAILED = "FAILED"; //$NON-NLS-1$

	private static final String OK = "OK"; //$NON-NLS-1$

	private Control control;

	private Contest contest;
	
	private List<Contest> contestList;

	private List<SelectItem> componentList;
	
	private ComponentConverter converter;
	
	private List<Component> selectedComponents;
	
	
	
	/**
	 * Default constructor of ContestBean. 
	 */
	public ContestBean() {
		this.control = new Control(new HibernateDAOFactory());
		setContest( new Contest() );
		listContests();
		listComponents();
		setSelectedComponents( new ArrayList<Component>() );
	}

	/**
	 * Returns the contest
	 * @return
	 */
	public Contest getContest(){
		return this.contest;
	}
	
	/**
	 * Setting the contest
	 * @param contest
	 */
	public void setContest(Contest contest) {
		this.contest = contest;
	}
	
	/**
	 * Navigation handling method (Action methods) that insert a contest. 
	 * @return "OK" if the insertion occur without problems, otherwise "FAILED".
	 */
	public String insertContest() {
		try {
			getContest().setComponent( getSelectedComponents() );
			getControl().insertContest( getContest() );
			for ( Component selectedComponent : getSelectedComponents() ) {
				selectedComponent.setContest( getContest() );
				getControl().updateComponent( selectedComponent );
			}
			setContest( new Contest() );
			return OK;
		} catch (FloAppException e) {
			return FAILED;
		} finally {
			listContests();
		}
	}	
	
	/**
	 * Returns the control
	 * @return the control
	 */
	public Control getControl() {
		return this.control;
	}

	/**
	 * Setting a control
	 * @param control - the control...
	 */
	public void setControl(Control control) {
		this.control = control;
	}
	
	public void listContests(){
		setContestList( getControl().listContests() );
		for ( Contest cont : getContestList() ) {
			System.out.println(cont);
		}
	}

	public void listComponents(){
		List<Component> components = getControl().listComponents();
		setComponentList( new ArrayList<SelectItem>() );
		for ( Component component : components ) {
			if ( component.getContest() == null ){
				getComponentList().add( new SelectItem( component, component.getName()) );
			}
		}
	}

	public String removeContest(){
		getControl().removeContest( getContest().getName() );
		listContests();
		return new MenuBarRedirection().listContestPage();
	}

	public List<Contest> getContestList() {
		return this.contestList;
	}

	public void setContestList(List<Contest> contestList) {
		this.contestList = contestList;
	}

	
	public List<SelectItem> getComponentList() {
		return this.componentList;
	}

	
	public void setComponentList( List<SelectItem> componentList ) {
		this.componentList = componentList;
	}

	
	public ComponentConverter getConverter() {
		listComponents();
		setConverter( new ComponentConverter( getComponentList() ) );
		return this.converter;
	}

	
	public void setConverter( ComponentConverter converter ) {
		this.converter = converter;
	}

	
	public List<Component> getSelectedComponents() {
		return this.selectedComponents;
	}

	
	public void setSelectedComponents( List<Component> selectedComponents ) {
		this.selectedComponents = selectedComponents;
	}

}
